package com.xxrl.shop.service;

import com.xxrl.shop.domain.*;
import com.xxrl.shop.repository.OrderRepository;
import com.xxrl.shop.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author mis
 */
@Service
public class OrderService {

    private final OrderRepository orderRepository;
    private final UserRatingService userRatingService;
    private final ProductRepository productRepository;

    @Autowired
    public OrderService(OrderRepository orderRepository, UserRatingService userRatingService, ProductRepository productRepository) {
        this.orderRepository = orderRepository;
        this.userRatingService = userRatingService;
        this.productRepository = productRepository;
//        dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    public int countOrder() {
        return (int) orderRepository.count();
    }

    public Page<Order> getOrderByMerchant(Merchant merchant, Pageable pageable) {
        return orderRepository.queryOrdersByMerchant(merchant, pageable);
    }

    public Page<Order> getOrderByUser(User user, Pageable pageable) {
        return orderRepository.queryOrdersByUser(user, pageable);
    }

    public Page<Order> listOrder(Pageable pageable) {
        return orderRepository.findAll(pageable);
    }

    public Page<Order> getOrderByProduct(Product product, Pageable pageable) {
        return orderRepository.queryOrdersByProduct(product, pageable);
    }

    public Page<Order> getOrderWithCommentByProduct(Product product, Pageable pageable) {
        return orderRepository.queryOrdersByProductAndCommentExists(product, pageable);
    }

    public Page<Order> getOrderAfterDateByProduct(Product product, Date date, Pageable pageable) {
        return orderRepository.queryOrdersByProductAndCreateTimeAfter(product, date, pageable);
    }

    public void updateOrderStatus(Long orderId, String status) {
        Order order = orderRepository.getOne(orderId);
        order.setPay(true);
        order.setStatus(status);
        orderRepository.save(order);
    }

    public void saveComment(Long orderId, Double rating, String comment) {
        Order order = orderRepository.getOne(orderId);
        order.setComment(comment);
        order.setRating(rating);
        order.setStatus("已评论");
        UserRating userRating = new UserRating();
        userRating.setProductId(order.getProduct().getId());
        userRating.setRating(rating);
        userRating.setUserId(order.getUser().getId());
        userRatingService.saveRating(userRating);
        orderRepository.save(order);
    }

    @CacheEvict(cacheNames = "homePageStatistics")
    public Long addOrder(Long productId, Long userId, Long addressId, int num) {
        Product product = productRepository.getOne(productId);
        Order order = new Order();
        order.setProduct(product);
        order.setUser(new User(userId));
        order.setAddress(new Address(addressId));
        order.setPay(false);
        order.setProductNum(num);
        order.setStatus("not pay");
        order.setComment("not comment yet");
        orderRepository.save(order);

        product.setSellCount(product.getSellCount()+1);
        productRepository.save(product);
        return order.getId();
    }

    public void saveOrder(Order order) {
        assert order.getId() != null ;
        orderRepository.save(order);
    }

    public void removeOrder(Long id) {
        orderRepository.delete(new Order(id));
    }
}
